using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TestXNA3.Games;
using TestXNA3.Render;
using System.Reflection;
using TestXNA3.Resources;


namespace TestXNA3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameStartup : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public GameInfo GameInfo;

        public static int Width = 1024;
        public static int Height = 600;
        public static ResourceManager ContentManager = null;
        public static GraphicsDevice Graphics = null;
        public static QuadRenderer QuadRenderer = null;

        public static StillDesign.PhysX.Core PhysXCore = null;
        public static GameStartup StaticGameStartup = null;

        public static Assembly CurrentAssembly = null;
        public static bool InEditor = false;
        public static GameTime GameTime;

        private ResourceManager _resourceManager;

        public GraphicsDeviceManager GraphicsManager
        {
            get { return graphics; }
        }

        public GameStartup()
        {
            StaticGameStartup = this;

            CurrentAssembly = Assembly.GetExecutingAssembly();

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = Width;
            graphics.PreferredBackBufferHeight = Height;

            _resourceManager = new ResourceManager(this.Services);
            Content = _resourceManager;

            Content.RootDirectory = TestConfig.ContentPath;

            if (PhysXCore != null)
                PhysXCore.Dispose();

            PhysXCore = new StillDesign.PhysX.Core();


            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizationScale, 1.0f);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeCollisionShapes, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeClothMesh, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeJointLocalAxes, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeJointLimits, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeFluidPosition, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeFluidEmitters, false); // Slows down rendering a bit to much
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeForceFields, true);
            //PhysXCore.SetParameter(StillDesign.PhysX.PhysicsParameter.VisualizeSoftBodyMesh, true);
        }

        protected override void EndRun()
        {
            if (PhysXCore != null)
            {
                PhysXCore.Dispose();
            }
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            ContentManager = _resourceManager;
            Graphics = graphics.GraphicsDevice;

            QuadRenderer = new QuadRenderer(this);
            Components.Add(QuadRenderer);

            GameInfo.Initialize();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            GameTime = gameTime;
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            double ms = (double)gameTime.ElapsedGameTime.Milliseconds;
            ms *= 0.001;
            GameInfo.Update((float)ms);

            GameInfo.TimeSeconds += ms;

            _resourceManager.Update(ms, GameInfo.TimeSeconds);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            float ms = (float)gameTime.ElapsedGameTime.Milliseconds;
            ms *= 0.001f;
            GameInfo.Draw(ms);

            base.Draw(gameTime);
        }
    }
}
